package com.zondy.mapgis.psmap.manager.controller;

import com.zondy.mapgis.common.controllerprefix.annotation.ManagerRestController;
import com.zondy.mapgis.common.core.utils.poi.ExcelUtil;
import com.zondy.mapgis.common.core.web.controller.BaseController;
import com.zondy.mapgis.common.core.web.domain.AjaxResult;
import com.zondy.mapgis.common.core.web.page.TableDataInfo;
import com.zondy.mapgis.common.log.annotation.Log;
import com.zondy.mapgis.common.log.enums.BusinessType;
import com.zondy.mapgis.common.security.annotation.RequiresPermissions;
import com.zondy.mapgis.domain.psmap.AppDistrict;
import com.zondy.mapgis.domain.psmap.AppDistrictGeoJSONDto;
import com.zondy.mapgis.psmap.manager.service.IAppDistrictService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Tag(
   name = "行政区划管理",
   description = "行政区划控制器"
)
@ManagerRestController({"/app/district"})
public class AppDistrictController extends BaseController {
   @Autowired
   private IAppDistrictService appDistrictService;

   @Operation(
      summary = "查询行政区划列表"
   )
   @PreAuthorize("@ss.hasPermi('app:district:list')")
   @RequiresPermissions({"app:district:list"})
   @GetMapping({"/list"})
   public TableDataInfo list(AppDistrict appDistrict) {
      this.startPage();
      List<AppDistrict> list = this.appDistrictService.selectAppDistrictList(appDistrict);
      return this.getDataTable(list);
   }

   @Operation(
      summary = "查询行政区划列表（树形结构）"
   )
   @PreAuthorize("@ss.hasPermi('app:district:list')")
   @RequiresPermissions({"app:district:list"})
   @GetMapping({"/tree"})
   public AjaxResult treeList(@RequestParam(name = "year",required = false) Integer year, @RequestParam(name = "level",required = false) Integer level, @RequestParam(name = "includeGeoJson",required = false,defaultValue = "false") Boolean includeGeoJson) {
      return this.success(this.appDistrictService.buildDistrictList(year, level, includeGeoJson));
   }

   @Operation(
      summary = "导出行政区划列表"
   )
   @PreAuthorize("@ss.hasPermi('app:district:export')")
   @RequiresPermissions({"app:district:export"})
   @Log(
      title = "行政区划",
      businessType = BusinessType.EXPORT
   )
   @PostMapping({"/export"})
   public void export(HttpServletResponse response, AppDistrict appDistrict) {
      List<AppDistrict> list = this.appDistrictService.selectAppDistrictList(appDistrict);
      ExcelUtil<AppDistrict> util = new ExcelUtil<>(AppDistrict.class);
      util.exportExcel(response, list, "行政区划数据");
   }

   @Operation(
      summary = "获取行政区划详细信息"
   )
   @PreAuthorize("@ss.hasPermi('app:district:query')")
   @RequiresPermissions({"app:district:query"})
   @GetMapping({"/{districtId}"})
   public AjaxResult getInfo(@PathVariable("districtId") Long districtId) {
      return this.success(this.appDistrictService.selectAppDistrictByDistrictId(districtId));
   }

   @Operation(
      summary = "新增行政区划"
   )
   @PreAuthorize("@ss.hasPermi('app:district:add')")
   @RequiresPermissions({"app:district:add"})
   @Log(
      title = "行政区划",
      businessType = BusinessType.INSERT
   )
   @PostMapping
   public AjaxResult add(@RequestBody AppDistrict appDistrict) {
      return this.toAjax(this.appDistrictService.insertAppDistrict(appDistrict));
   }

   @Operation(
      summary = "修改行政区划"
   )
   @PreAuthorize("@ss.hasPermi('app:district:edit')")
   @RequiresPermissions({"app:district:edit"})
   @Log(
      title = "行政区划",
      businessType = BusinessType.UPDATE
   )
   @PutMapping
   public AjaxResult edit(@RequestBody List<AppDistrict> appDistricts) {
      return this.toAjax(this.appDistrictService.updateAppDistricts(appDistricts));
   }

   @Operation(
      summary = "修改行政区划"
   )
   @PreAuthorize("@ss.hasPermi('app:district:edit')")
   @RequiresPermissions({"app:district:edit"})
   @Log(
      title = "行政区划",
      businessType = BusinessType.UPDATE
   )
   @PutMapping({"/geojson"})
   public AjaxResult editGeoJSON(@Validated @RequestBody AppDistrictGeoJSONDto appDistrictGeoJSONDto) {
      return this.toAjax(this.appDistrictService.updateGeoJSON(appDistrictGeoJSONDto));
   }

   @Operation(
      summary = "删除行政区划"
   )
   @PreAuthorize("@ss.hasPermi('app:district:remove')")
   @RequiresPermissions({"app:district:remove"})
   @Log(
      title = "行政区划",
      businessType = BusinessType.DELETE
   )
   @DeleteMapping({"/{districtIds}"})
   public AjaxResult remove(@PathVariable Long[] districtIds) {
      return this.toAjax(this.appDistrictService.deleteAppDistrictByDistrictIds(districtIds));
   }

   @Operation(
      summary = "导入Excel行政区划数据"
   )
   @PreAuthorize("@ss.hasPermi('app:district:import')")
   @RequiresPermissions({"app:district:import"})
   @Log(
      title = "行政区划",
      businessType = BusinessType.IMPORT
   )
   @PostMapping({"/import"})
   public AjaxResult importExcel(@RequestParam("file") MultipartFile file) {
      int res = this.appDistrictService.importExcel(file);
      return res > 0 ? this.success() : this.error();
   }
}
